
import { _decorator, Component, Node, Sprite, SpriteFrame, CCBoolean } from 'cc';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = GifPlayer
 * DateTime = Sat Feb 05 2022 15:07:04 GMT+0800 (China Standard Time)
 * Author = vvc_zeng
 * FileBasename = GifPlayer.ts
 * FileBasenameNoExtension = GifPlayer
 * URL = db://assets/script/utils/GifPlayer.ts
 * ManualUrl = https://docs.cocos.com/creator/3.3/manual/zh/
 *
 */

@ccclass('GifPlayer')
export class GifPlayer extends Component {
    sprite: Sprite = null;
    index: number = 0;
    interval: number = 0.08;
    @property([SpriteFrame])
    frames: SpriteFrame[] = [];
    @property(CCBoolean)
    forever: boolean = false;
    onLoad() {
        this.sprite = <Sprite>this.getComponent("cc.Sprite")
    }
    start() {
    }
    onEnable() {
        this.schedule(this.onTimer, this.interval);
    }
    onDisable() {
        this.unschedule(this.onTimer);
    }
    onTimer() {
        if (this.frames.length < 1) {
            return;
        }
        this.sprite.spriteFrame = this.frames[this.index];
        this.index++;
        if (this.index >= this.frames.length) {
            this.index = 0;
            if (!this.forever) {
                this.unschedule(this.onTimer);
                this.node.active = false;
            }
        }
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.3/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.3/manual/zh/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.3/manual/zh/scripting/life-cycle-callbacks.html
 */
